package com.ali.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 自定义全局处理器
 * ControllerAdvice：Controller增强器
 */
@Slf4j
@ControllerAdvice
public class MyGlobalHandler {
    /**
     * 全局异常处理
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public String globalExceptionHandle(Exception e) {
        log.error(MyExceptionHandler.exceptionHandle(e));
        return e.getMessage();
    }

    /**
     * 添加参数
     */
    @ModelAttribute
    public void addAttributes1(Model model) {
        model.addAttribute("addParam1", "增强器添加的参数");
    }

    /**
     * 添加参数
     */
    @ModelAttribute(value = "addParam2")
    public String addAttributes2() {
        return "增强器添加的参数";
    }
}
